﻿Imports System.IO
Imports DynamicControls
Imports System.Configuration
Partial Class Admin_CreateQuestion
    'Inherits System.Web.UI.Page
    Inherits ClientSidePage
    Protected Sub PageLoad(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Populate the list of user controls on page load
            PopulateUserControlDdl()
        End If
    End Sub
    Private Sub PopulateUserControlDdl()
        'Get a list of FileInfos on files with the .ascx extension in the Building Block User Controls directory
        Dim dirInfo As New DirectoryInfo(Server.MapPath(ResolveUrl(ConfigurationSettings.AppSettings("buildingBlockPath"))))

        'Now bind the list of files to the DropDownList
        controlSrc.DataSource = dirInfo.GetFiles("*.ascx")
        controlSrc.DataBind()
    End Sub
    Protected Sub BtnCancelClick(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("Default.aspx")
    End Sub
    Protected Sub BtnAddQuestionClick(sender As Object, e As System.EventArgs) Handles btnAddQuestion.Click
        'Add a new record
        Dim hotDeal As New App_Code.HotDeal()

        If controlSrc.SelectedValue.Equals("DQOptionQuestion.ascx") Then
            '''If OptionQuestion is Selected to add
            Dim optionId = hotDeal.InsertNewQuestion(questionText.Text, controlSrc.SelectedItem.Value, CType(txtDefaultValue.Text, Byte), CType(txtAnswerValue.Text, Byte))
            hidOptionID.Value = optionId.ToString()
            If optionId > 0 Then
                InsertOptionChoices(optionId)
            End If
        Else
            hotDeal.InsertNewQuestion(questionText.Text, controlSrc.SelectedItem.Value)
        End If
        'SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connectionString"), _
        '        CommandType.StoredProcedure, "dq_AddQuestion", _
        '        New SqlParameter("@QuestionText", questionText.Text), _
        '        New SqlParameter("@ControlSrc", controlSrc.SelectedItem.Value))

        Response.Redirect("Default.aspx")       'send them back to the admin page
    End Sub
    Protected Sub BtnRefreshUserControlListClick(sender As Object, e As System.EventArgs) Handles btnRefreshUserControlList.Click
        PopulateUserControlDdl()
    End Sub
    Protected Sub LnkPreviewClick(sender As Object, e As System.EventArgs) Handles lnkPreview.Click
        DisplayPopup(String.Concat("PreviewUC.aspx?Src=", Server.UrlEncode(controlSrc.SelectedItem.Value)) & String.Concat("&Question=", questionText.Text), "toolbar=no,resizable=yes,scrollbars=yes")
    End Sub
    Protected Sub ControlSrcSelectedIndexChanged(sender As Object, e As System.EventArgs) Handles controlSrc.SelectedIndexChanged
        'Display Contrlol Edit Page When user select ControlSrc
        If controlSrc.SelectedValue.Equals("DQOptionQuestion.ascx") Then
            Div_AddNewOption.Visible = True
        Else
            Div_AddNewOption.Visible = False
        End If
    End Sub
    Private Sub InsertOptionChoices(ByVal optionId As Integer)
        Dim objPoll As New App_Code.HotDeal()
        'lopping through all the form keyvalues to find input texts
        Dim choiceValue As Integer = 1
        For Each key As String In From key1 As String In Request.Form Where key1.IndexOf("txtChoice") >= 0 Where Request.Form(key1).Trim().Length > 0
            objPoll.InsertOptionChoices(optionId, Request.Form(key).Trim(), CType(choiceValue, Byte))
            choiceValue += 1
        Next
    End Sub

End Class
